// JavaScript Document
Class("AnimationWrapper", {
	has:{
		node: {is: "rw"},
		duration: {is: "rw"},
		delay: {is: "rw"},
		loop: {is: "rw"},
		kineticAnimation: {is: "rw"},
		functionToCallback: {is: "rw"},
		callbackArgs: {is: "rw"},
		startTime: {is: "rw"},
		startedAttrs: {
			is: "ro",
			init: function(){return $.extend({}, this.node.getAttrs());}
			},
		numberOfFrames: {
			is: "ro",
			init: function(){return this.duration*60/1000;}
			},
		endAttrs:{
			is: "ro"
			}
		},
		
	methods: {
		createAnimation: function(name, args){
			var _this = this;
			var func = null;
			
			switch(name){
				case "FadeOut":{
					func = function(frame){
						var currentTime = new Date().getTime();
						var time = currentTime - _this.startTime;
						var a = Math.pow(time / _this.duration , 2);
						var opac = _this.startedAttrs["opacity"] - (_this.startedAttrs["opacity"] - args["opacity"]) * a;
						//var opac = (_this.startedAttrs["opacity"] - time / _this.duration > 0) ? (_this.startedAttrs.opacity - time / _this.duration) : 0;
						_this.node.setOpacity(opac);
						}
					}
					break;
					
				case "FadeIn":{
					func = function(frame){
						var currentTime = new Date().getTime();
						var time = currentTime - _this.startTime;
						var a = Math.pow(time / _this.duration , 2);
						var opac = _this.startedAttrs["opacity"] + (_this.startedAttrs["opacity"] + args["opacity"]) * a;
						//var opac = (time / _this.duration <= 1) ? (time / _this.duration) : 1;
						_this.node.setOpacity(opac);
						}
					}
					break;
					
				case "ZoomIn":{
					func = function(frame){
						var currentTime = new Date().getTime();
						var time = currentTime - _this.startTime;
						//var opac = (_this.startedAttrs.opacity - time / _this.duration > 0) ? (_this.startedAttrs.opacity - time / _this.duration) : 0;
						//_this.node.setOffset(_this.node.getWidth()/2, _this.node.getHeight()/2);
						//_this.node.setOpacity(opac);
						var a = Math.pow(time / _this.duration, 2);
						var dScaleX = (args["scaleX"] - _this.startedAttrs.scale.x) * a;
						var dScaleY = (args["scaleY"] - _this.startedAttrs.scale.y) * a;
						_this.node.setScale(_this.startedAttrs.scale.x + dScaleX, _this.startedAttrs.scale.y + dScaleY);
						}
					}
					break;
				
				case "ZoomOut":{
					func = function(frame){
						var currentTime = new Date().getTime();
						var time = currentTime - _this.startTime;
						//var opac = (_this.startedAttrs.opacity - time / _this.duration > 0) ? (_this.startedAttrs.opacity - time / _this.duration) : 0;
						//_this.node.setOffset(_this.node.getWidth()/2, _this.node.getHeight()/2);
						//_this.node.setOpacity(opac);
						var a = Math.pow(time / _this.duration, 2);
						var dScaleX = (_this.startedAttrs.scale.x - args["scaleX"]) * a;
						var dScaleY = (_this.startedAttrs.scale.y - args["scaleY"]) * a;
						_this.node.setScale(_this.startedAttrs.scale.x - dScaleX, _this.startedAttrs.scale.y - dScaleY);
						}
					}
					break;
				
				case "RotateRight": {
					func = function(frame){
						var currentTime = new Date().getTime();
						var time = currentTime - _this.startTime;
						var newRotatingTheta = ( 3 * Math.PI * time ) / _this.duration;
						_this.node.rotate(newRotatingTheta - _this.node.getRotation());
						}
					}
				}
				
			this.kineticAnimation = new Kinetic.Animation(func, args["layer"]);
			//this.kineticAnimation = new Kinetic.Animation(func, _this.node.getParent().getParent());
			},
		start: function(){
			var _this = this;
			
			if(_this.kineticAnimation != null){
				
				var startAni = function(){
					setTimeout(function(){
						_this.startTime = new Date().getTime();
						_this.kineticAnimation.start();
						}, _this.delay)
					}
					
				startAni();
				
				$.doTimeout(this.duration + this.delay, function(){
					_this.stop();
					if(_this.functionToCallback != null && typeof _this.functionToCallback == "function"){
						if(_this.callbackArgs != null){
							_this.functionToCallback(_this.callbackArgs);
							}
						else{
							_this.functionToCallback();
							}
						}
					if(_this.loop){
						_this.node.setAttrs(_this.startedAttrs);
						startAni();
						return true;
						}
					else{
						return false;
						}
					});
				}
			},
		stop: function(){
			if(this.kineticAnimation != null){
				this.kineticAnimation.stop();
				}
			},
		setCallback: function(f, args){
			this.functionToCallback = f;
			this.callbackArgs = args;
			}
		}
	});